PHP标准库 (SPL) 笔记

您所在的位置:网站首页 阮一峰 inode PHP标准库 (SPL) 笔记

PHP标准库 (SPL) 笔记

2022-12-02 00:40| 来源: 网络整理| 查看: 265

SPL是Standard PHP Library(PHP标准库)的缩写。

The Standard PHP Library (SPL) is a collection of interfaces and classes that are meant to solve common problems.

官网说,SPL是用来解决典型问题(common problems)的一组接口与类的集合。

那么,什么是common problems呢?

- 数据结构 解决数据怎么存储问题 - 元素遍历 数据怎么查看 - 常用方法的统一调用 数组、集合大小 自定义遍历 - 类自动加载 spl_autoload_register

包含哪些内容?

数据结构 基础接口 基础函数 迭代器 异常 其它 SPL接口 Iterator 迭代器接口

SPL规定,所有实现了Iterator接口的class,都可以用在foreach Loop中。Iterator接口中包含5个必须实现的方法:

interface Iterator extends Traversable{ //返回当前元素 public mixed current ( void ); //返回当前元素的键 public scalar key ( void ); //向前移动到下一个元素 public void next ( void ); //返回到迭代器的第一个元素 public void rewind ( void ); //检查当前位置是否有效 public boolean valid ( void ); } ArrayAccess 数组式访问接口

实现ArrayAccess接口,可以使得object像array那样操作。ArrayAccess接口包含四个必须实现的方法:

interface ArrayAccess { //检查一个偏移位置是否存在 public mixed offsetExists ( mixed $offset ); //获取一个偏移位置的值 public mixed offsetGet( mixed $offset ); //设置一个偏移位置的值 public mixed offsetSet ( mixed $offset ); //复位一个偏移位置的值 public mixed offsetUnset ( mixed $offset ); } IteratorAggregate 聚合式迭代器接口

假设对象A实现了上面的ArrayAccess接口,这时候虽然可以像数组那样操作,却无法使用foreach遍历,除非实现了前面提到的Iterator接口。

另一个解决方法是,有时会需要将数据和遍历部分分开,这时就可以实现IteratorAggregate接口。它规定了一个getIterator()方法,返回一个使用Iterator接口的object。

IteratorAggregate extends Traversable { /* 获取一个外部迭代器 */ abstract public Traversable getIterator ( void ) }

示例:

注意:虽然都继承自Traversable,但这是一个无法在 PHP 脚本中实现的内部引擎接口。我们直接使用IteratorAggregate 或 Iterator 接口来代替它。

RecursiveIterator

这个接口用于遍历多层数据,它继承了Iterator接口,因而也具有标准的current()、key()、next()、 rewind()和valid()方法。同时,它自己还规定了getChildren()和hasChildren()方法。The getChildren() method must return an object that implements RecursiveIterator。

SeekableIterator

SeekableIterator接口也是Iterator接口的延伸,除了Iterator的5个方法以外,还规定了seek()方法,参数是元素的位置,返回该元素。如果该位置不存在,则抛出OutOfBoundsException。

Countable

这个接口规定了一个count()方法,返回结果集的数量。

SPL数据结构

数据结构是计算机存储、组织数据的方式。

SPL提供了双向链表、堆栈、队列、堆、降序堆、升序堆、优先级队列、定长数组、对象容器。

基本概念Bottom:节点,第一个节点称Bottom;Top:最后添加的链表的节点称Top;当前节点(Current):链表指针指向的节点称为当前节点;

SplDoublyLinkedList 双向链表

SplDoublyLinkedList 实现了Iterator , ArrayAccess , Countable接口。

类摘要

SplDoublyLinkedList implements Iterator , ArrayAccess , Countable { /* 方法 */ public __construct ( void ) public void add ( mixed $index , mixed $newval ) public mixed bottom ( void ) public int count ( void ) public mixed current ( void ) public int getIteratorMode ( void ) public bool isEmpty ( void ) public mixed key ( void ) public void next ( void ) public bool offsetExists ( mixed $index ) public mixed offsetGet ( mixed $index ) public void offsetSet ( mixed $index , mixed $newval ) public void offsetUnset ( mixed $index ) public mixed pop ( void ) public void prev ( void ) public void push ( mixed $value ) public void rewind ( void ) public string serialize ( void ) public void setIteratorMode ( int $mode ) public mixed shift ( void ) public mixed top ( void ) public void unserialize ( string $serialized ) public void unshift ( mixed $value ) public bool valid ( void ) }

注意:SplDoublyLinkedList::setIteratorMode用来设置链表模式:

迭代方向:

SplDoublyLinkedList::IT_MODE_LIFO (Stack style) SplDoublyLinkedList::IT_MODE_FIFO (Queue style)

迭代器行为:

SplDoublyLinkedList::IT_MODE_DELETE (Elements are deleted by the iterator) SplDoublyLinkedList::IT_MODE_KEEP (Elements are traversed by the iterator)

默认模式: SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP

当前节点操作:rewind:将链表的当前指针指向第一个元素current:链表当前指针,当节点被删除后,会指向空节点prev:上一个next:下一个

增加节点操作:push 在双向链表的结尾处将元素压入unshift 前置双链表元素,预备值在双链表的开始

删除节点操作:pop 从双向链表的结尾弹出一个节点,不会改变指针位置shift从双向链表的开头弹出一个节点,不会改变指针位置

定位操作:bottom 返回当前双向链表的第一个节点的值,当前指针不变top返回当前双向链表的最后一个节点的值,当前指针不变

特定节点操作:offsetExists 理解为key是否存在offsetGet将key节点拿出来offsetSet把数据刷新offsetUnset删除

示例:SplDoublyLinkedList.php

SplFileInfo

PHP SPL中提供了SplFileInfo和SplFileObject两个类来处理文件操作。

SplFileInfo用来获取文件详细信息:

$file = new SplFileInfo('foo-bar.txt'); print_r(array( 'getATime' => $file->getATime(), //最后访问时间 'getBasename' => $file->getBasename(), //获取无路径的basename 'getCTime' => $file->getCTime(), //获取inode修改时间 'getExtension' => $file->getExtension(), //文件扩展名 'getFilename' => $file->getFilename(), //获取文件名 'getGroup' => $file->getGroup(), //获取文件组 'getInode' => $file->getInode(), //获取文件inode 'getLinkTarget' => $file->getLinkTarget(), //获取文件链接目标文件 'getMTime' => $file->getMTime(), //获取最后修改时间 'getOwner' => $file->getOwner(), //文件拥有者 'getPath' => $file->getPath(), //不带文件名的文件路径 'getPathInfo' => $file->getPathInfo(), //上级路径的SplFileInfo对象 'getPathname' => $file->getPathname(), //全路径 'getPerms' => $file->getPerms(), //文件权限 'getRealPath' => $file->getRealPath(), //文件绝对路径 'getSize' => $file->getSize(),//文件大小,单位字节 'getType' => $file->getType(),//文件类型 file dir link 'isDir' => $file->isDir(), //是否是目录 'isFile' => $file->isFile(), //是否是文件 'isLink' => $file->isLink(), //是否是快捷链接 'isExecutable' => $file->isExecutable(), //是否可执行 'isReadable' => $file->isReadable(), //是否可读 'isWritable' => $file->isWritable(), //是否可写 )); SplFileObject

SplFileObject继承SplFileInfo并实现RecursiveIterator、 SeekableIterator接口 ,用于对文件遍历、查找、操作遍历:

try { foreach(new SplFileObject('foo-bar.txt') as $line) { echo $line; } } catch (Exception $e) { echo $e->getMessage(); }

查找指定行:

try { $file = new SplFileObject('foo-bar.txt'); $file->seek(2); echo $file->current(); } catch (Exception $e) { echo $e->getMessage(); }

写入csv文件:

$list = array ( array( 'aaa' , 'bbb' , 'ccc' , 'dddd' ), array( '123' , '456' , '7891' ) ); $file = new SplFileObject ( 'file.csv' , 'w' ); foreach ( $list as $fields ) { $file -> fputcsv ( $fields ); } DirectoryIterator

该类继承自SplFileInfo并实现SeekableIterator接口。

这个类用来查看一个目录中的所有文件和子目录:

ArrayObject 该类实现了ArrayAccess ,Countable, IteratorAggregate, Serializable接口。 这个类可以将Array转化为object。 ArrayIterator

该类实现了ArrayAccess, Countable , SeekableIterator , Serializable 接口。

这个类实际上是对ArrayObject类的补充,为后者提供遍历功能。

参考1、PHP: SPL - Manualhttp://php.net/manual/zh/book.spl.php2、PHP: 预定义接口 - Manualhttp://php.net/manual/zh/reserved.interfaces.php3、PHP SPL笔记 - 阮一峰的网络日志http://www.ruanyifeng.com/blog/2008/07/php_spl_notes.html4、PHP SPL标准库之文件操作(SplFileInfo和SplFileObject) - PHP点点通http://www.phpddt.com/php/SplFileObject.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3